<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>

    <el-button type="text" @click="openChapterDialog()">添加章节</el-button>

    <!-- 章节 -->
    <ul class="chanpterList">
        <li
            v-for="chapter in chapterVideoList"
            :key="chapter.id">
            <p>
                {{ chapter.title }}

                <span class="acts">
                    <el-button style="" type="text" @click="openVideo(chapter.id)">添加小节</el-button>
                    <el-button style="" type="text" @click="openEditChatper(chapter.id)">编辑</el-button>
                    <el-button type="text" @click="removeChapter(chapter.id)">删除</el-button>
                </span>
            </p>

            <!-- 视频 -->
            <ul class="chanpterList videoList">
                <li
                    v-for="video in chapter.children"
                    :key="video.id">
                    <p>{{ video.title }}

                <span class="acts">
                    
                    <el-button style="" type="text" @click="openEditVideo(video.id)">编辑</el-button>
                    <el-button type="text" @click="removeVideo(video.id)">删除</el-button>
                </span>
                    </p>
                </li>
            </ul>
        </li>
    </ul>
    <div>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
    </div>

    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
        <el-form :model="chapter" label-width="120px">
            <el-form-item label="章节标题">
                <el-input v-model="chapter.title"/>
            </el-form-item>
            <el-form-item label="章节排序">
                <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
        </div>
    </el-dialog>

    <!-- 添加和修改课时表单 -->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
      <el-form :model="video" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="video.isFree">
            <el-radio :label="true">免费</el-radio>
            <el-radio :label="false">默认</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上传视频">
          <el-upload
                :on-success="handleVodUploadSuccess"
                :on-remove="handleVodRemove"
                :before-remove="beforeVodRemove"
                :on-exceed="handleUploadExceed"
                :file-list="fileList"
                :action="BASE_API+'/eduvod/video/upload'"
                :limit="1"
                class="upload-demo">
          <el-button size="small" type="primary">上传视频</el-button>
          <el-tooltip placement="right-end">
              <div slot="content">最大支持1G，<br>
                  支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
                  GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
                  MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
                  SWF、TS、VOB、WMV、WEBM 等视频格式上传</div>
              <i class="el-icon-question"/>
          </el-tooltip>
          </el-upload>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import chapterApi from '@/api/edu/chapter'
import videoApi from '@/api/edu/video'
import vodApi from '@/api/edu/vod'
export default {
  data() {
    return {
      BASE_API : process.env.BASE_API,  // 将config里面保存的BASE_API取出来，固定写法
      saveBtnDisabled: false, // 保存按钮是否禁用
      saveVideoBtnDisabled: false, // 保存按钮是否禁用
      courseId:null,
      dialogChapterFormVisible:false, // 添加章节标志位
      dialogVideoFormVisible: false, // 添加小节标志位
      chapter:{},
      video:{},
      chapterVideoList:null,
      fileList:[],
    }
  },

  created() {
    console.log('chapter created')
    this.init()

  },

  methods: {
    init() {
      if (this.$route.params && this.$route.params.id) {
              this.courseId = this.$route.params.id
        }
        // 初始化list
      this.getChapterVideoList()
    },
    getChapterVideoList(){
      chapterApi.getChapterVideoTree(this.courseId)
        .then(response => {
          this.chapterVideoList = response.data.data
        })
    },

    previous() {
      console.log('previous')
      this.$router.push({ path: `/course/info/${this.courseId}` })
    },

    next() {
      console.log('next')
      this.$router.push({ path: `/course/publish/${this.courseId}` })
    },

    // 添加章节触发
    openChapterDialog() {
      this.chapter = {courseId: this.courseId}
      this.dialogChapterFormVisible = true  // 触发添加弹窗
    },
    // 编辑章节触发
    openEditChatper(chapterId) {
      this.getChapter(chapterId)
      this.dialogChapterFormVisible = true  // 触发编辑弹窗
    },

    getChapter(chapterId) {
      chapterApi.getChapter(chapterId)
        .then(response => {
          this.chapter = response.data.data
        })
    },

    // 添加或者编辑保存
    saveOrUpdate() {
      if (this.chapter.id) {
        this.updateChapter()
      } else {
        this.saveChapter()
      }
      this.dialogChapterFormVisible = false
    },

    saveChapter() {
      chapterApi.addChapter(this.chapter)
        .then(response => {
          if (response.success === true) {
            this.$message({
                    type: 'success',
                    message: '保存成功!'
            });
            this.getChapterVideoList();
          } else {
            this.$message({
                    type: 'error',
                    message: '保存失败!'
            });
          }
        })
    },

    updateChapter() {
      chapterApi.updateChapter(this.chapter)
        .then(response => {
          if (response.success === true) {
            this.$message({
                    type: 'success',
                    message: '保存成功!'
            });
            this.getChapterVideoList();
          } else {
            this.$message({
                    type: 'error',
                    message: '保存失败!'
            });
          }
        })
    },

    // 删除章节触发
    removeChapter(chapterId) {
      chapterApi.removeChapter(chapterId)
        .then(response => {
          if (response.success === true) {
            this.$message({
                    type: 'success',
                    message: '删除成功!'
            });
            this.getChapterVideoList();
          } else {
            this.$message({
                    type: 'error',
                    message: '删除失败!'
            });
          }
        })
    },
    // 删除小节触发
    removeVideo(videoId) {
      videoApi.removeVideo(videoId)
        .then(response => {
          if (response.success === true) {
            this.$message({
                    type: 'success',
                    message: '删除成功!'
            });
            this.getChapterVideoList();
          } else {
            this.$message({
                    type: 'error',
                    message: '删除失败!'
            });
          }
        })
    },

    getVideo(videoId) {
      videoApi.getVideo(videoId)
        .then(response => {
          this.video = response.data.data
          this.fileList = [{'name': this.video.videoOriginalName}]
        })
    },
    // 添加小节触发
    openVideo(chapterId) {
      this.video = {courseId: this.courseId, chapterId:chapterId}
      this.dialogVideoFormVisible = true
    },
    // 编辑小节
    openEditVideo(videoId) {
      this.dialogVideoFormVisible = true
      this.getVideo(videoId)
    },

    // 添加或者编辑小节
    saveOrUpdateVideo() {
      if (this.video.id) {
        this.updateVideo()
      } else {
        this.saveVideo()
      }
      this.dialogVideoFormVisible = false
    },
    saveVideo() {
      videoApi.addVideo(this.video)
        .then(response => {
          if (response.success === true) {
            this.$message({
                    type: 'success',
                    message: '保存成功!'
            });
            this.getChapterVideoList();
          } else {
            this.$message({
                    type: 'error',
                    message: '保存失败!'
            });
          }
        })
    },
    updateVideo() {
      console.log(this.video)
      videoApi.updateVideo(this.video)
        .then(response => {
          if (response.success === true) {
            this.$message({
                    type: 'success',
                    message: '保存成功!'
            });
            this.getChapterVideoList();
          } else {
            this.$message({
                    type: 'error',
                    message: '保存失败!'
            });
          }
        })
    },
    //成功回调
    handleVodUploadSuccess(response, file, fileList) {
      this.video.videoSourceId = response.data.videoId
      this.video.videoOriginalName = file.name
    },
    //视图上传多于一个视频
    handleUploadExceed(files, fileList) {
      this.$message.warning('想要重新上传视频，请先删除已上传的视频')
    },
    beforeVodRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleVodRemove(file, fileList) {
      console.log(file)
      vodApi.removeVideo(this.video.videoSourceId).then(response=>{
        this.$message({
          type: 'success',
          message: response.message
        })
        this.video.videoSourceId = null
        this.video.videoOriginalName = null
      })
    },
  }
}
</script>